package com.example.studydemo.database;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

import com.example.studydemo.entity.User;

public class UserDBHelper extends SQLiteOpenHelper {

    private static final String DB_NAME = "user.db";
    private static final String TABLE_NAME = "userInfo";
    private static final int DB_VERSION = 1;

    private static UserDBHelper mHelper = null;

    private SQLiteDatabase mRDB = null;
    private SQLiteDatabase mWDB = null;

    public UserDBHelper(Context context) {
        /** 子类传参给父类 */
        super(context,DB_NAME,null,DB_VERSION);
    }

    /** 采用单例模式获取数据库帮助器的唯一实例 */
    public static UserDBHelper getInstance(Context context){
        if (mHelper == null){
            mHelper = new UserDBHelper(context);
        }
        return mHelper;
    }

    /** 创建数据库，执行建表语句 */
    @Override
    public void onCreate(SQLiteDatabase db) {
        String sql = "CREATE TABLE IF NOT EXISTS " + TABLE_NAME + " (" +
                "_id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL," +
                " name VARCHAR NOT NULL," +
                " age INTEGER NOT NULL," +
                " height LONG NOT NULL," +
                " weight FLOAT NOT NULL," +
                " married INTEGER NOT NULL);";
        db.execSQL(sql);
    }

    /** 打开数据库的读连接 */
    public SQLiteDatabase openReadLink(){
        if (mRDB == null || !mRDB.isOpen()){
            mRDB = mHelper.getReadableDatabase();
        }
        return mRDB;
    }

    /** 打开数据库的写连接 */
    public SQLiteDatabase openWriteLink(){
        if (mWDB == null || !mWDB.isOpen()){
            mWDB = mHelper.getWritableDatabase();
        }
        return mWDB;
    }

    /** 关闭数据库连接 ,把读和写都关掉*/
    public void closeLink(){
        if (mRDB !=null && mRDB.isOpen()){
            mRDB.close();
            mRDB=null;
        }
        if (mWDB !=null && mWDB.isOpen()){
            mWDB.close();
            mWDB=null;
        }
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }
    public long insert(User user){
        ContentValues values = new ContentValues();
        values.put("name", user.name);
        values.put("age", user.age);
        values.put("height", user.height);
        values.put("weight", user.weight);
        values.put("married", user.married);
        return mWDB.insert(TABLE_NAME,null,values);
    }
}
